#include <SoftwareSerial.h>


SoftwareSerial softserial(6, 5);
int RxLCD = 6;
int TxLCD = 5;
int ledPin = 13;                // choose the pin for the LED
int KeyOK = 8;
int KeyESC = 9;
int EncCHA = 2;                // choose the input pin (for a pushbutton)
int EncCHB = 3;                // choose the input pin (for a pushbutton)
int EncValue = 0;
int EncPulse = 1;
int EncDivider = 10;
int EncDividerCountUP = 0;
int EncDividerCountDW = 0;
int TopScreen =0;
int OldTopScreen=0;
char* Menu1[] = {" DMX Channel        ", 
                 " Fixture            ", 
                 " Scene              ",
                 " Step               ",
                 " Setup              ", 
                 " Another Menu       ",
                 " Whynot             ",
                 ""};
char* Menu2[] = {" DMX Channel        ", 
                 " Fixture            ", 
                 " Scene              ",
                 " Step               ",
                 " Setup              ", 
                 " Another Menu       ",
                 " Whynot             ",
                 ""};
char* Menu3[] = {" DMX Channel        ", 
                 " Fixture            ", 
                 " Scene              ",
                 " Step               ",
                 " Setup              ", 
                 " Another Menu       ",
                 " Whynot             ",
                 ""};
int Choix;
int TapAvg[10];
int ReadTimeTap;
int TapSyncTime;
int LastTapSyncTime;
void Rotary()
{
//digitalWrite(ledPin, 1-digitalRead(ledPin));  // toggle LED
      if (digitalRead(EncCHB))
        { 
        // Up Direction High Speed  
        if (EncDividerCountUP++ > EncDivider)
          {
          EncDividerCountUP=0;
          EncValue++;
          EncPulse =1;
            if (EncValue >3)
              {
                
              EncValue = 3;  
              TopScreen++;
              if (TopScreen > 3)
                TopScreen = 3;
              }
          }
        }
      else 
        {
        // Down direction  
        if (EncDividerCountDW++ > EncDivider)
          {
          EncDividerCountDW = 0;  
          EncPulse =1;
          if (EncValue)
            {
            EncValue--;  
            }
          else
            {
            if (TopScreen)
              TopScreen--;
            }
          }        
        }
      

}

void TapSync()
{
ReadTimeTap = millis();
//digitalWrite(ledPin,1-digitalRead(ledPin));
TapSyncTime = ReadTimeTap-LastTapSyncTime;
LastTapSyncTime = ReadTimeTap;
}

void setup()
{
//Config of the rotary encoder
pinMode(EncCHA, INPUT);
pinMode(EncCHB, INPUT);
pinMode(1, OUTPUT);
pinMode(ledPin, OUTPUT);      // declare LED as output
pinMode(TxLCD, OUTPUT);
pinMode(KeyOK, INPUT);
pinMode(KeyESC, INPUT);
digitalWrite(KeyOK, HIGH); // Turn ON the pull up resistor
digitalWrite(KeyESC, HIGH);

digitalWrite(TxLCD, HIGH);
delay (100);
softserial.begin(9600);
Serial.begin(9600);
delay (100);
softserial.print(22, BYTE); // turn on LCD, No Cursor
softserial.print(17, BYTE); // turn on back light
delay (50);
softserial.print(12, BYTE); // Clear Screen
softserial.print(" DMX Console V1.0");
delay (1000);
softserial.print(12, BYTE); // Clear Screen
softserial.print(" Channel");
softserial.print(0x0D, BYTE); // LineFeed
softserial.print(" Fixture");
softserial.print(0x0D, BYTE); //
softserial.print(" Setup");
softserial.print(0x0D, BYTE); //
softserial.print(" Record");
interrupts();
attachInterrupt(0,Rotary,RISING);
//attachInterrupt(1,TapSync,RISING);


}





int ScrollMenu(char *Menu[])
{
  int i,j;
  noInterrupts();
  if (OldTopScreen != TopScreen)
    {
    OldTopScreen = TopScreen;
    softserial.print(0x80, BYTE);
    j=0;
    for (i = TopScreen;j<4 && Menu[i];i++)
      {
      j++;
      if (Menu[i])
        softserial.print(Menu[i]);
      }
    }
  if (EncValue == 0)
    {
    softserial.print(0x80, BYTE);
    softserial.print(0x7E, BYTE);
    softserial.print(0x94, BYTE);
    softserial.print(0x20, BYTE);
    softserial.print(0xA8, BYTE);
    softserial.print(0x20, BYTE);
    softserial.print(0xBC, BYTE);
    softserial.print(0x20, BYTE);
    }
  if (EncValue == 1)
    {
    softserial.print(0x80, BYTE);
    softserial.print(0x20, BYTE);
    softserial.print(0x94, BYTE);
    softserial.print(0x7E, BYTE);
    softserial.print(0xA8, BYTE);
    softserial.print(0x20, BYTE);
    softserial.print(0xBC, BYTE);
    softserial.print(0x20, BYTE);
    }
  if (EncValue == 2)
    {
    softserial.print(0x80, BYTE);
    softserial.print(0x20, BYTE);
    softserial.print(0x94, BYTE);
    softserial.print(0x20, BYTE);
    softserial.print(0xA8, BYTE);
    softserial.print(0x7E, BYTE);
    softserial.print(0xBC, BYTE);
    softserial.print(0x20, BYTE);
    }
  if (EncValue == 3)
    {
    softserial.print(0x80, BYTE);
    softserial.print(0x20, BYTE);
    softserial.print(0x94, BYTE);
    softserial.print(0x20, BYTE);
    softserial.print(0xA8, BYTE);
    softserial.print(0x20, BYTE);
    softserial.print(0xBC, BYTE);
    softserial.print(0x7E, BYTE);
    }
  interrupts();
  if (!digitalRead(KeyOK))
    {
    return (TopScreen+EncValue);
    }
  else if (!digitalRead(KeyESC))
    {
    return (254);
    }
  else
    return (255);
}
void loop()
{
digitalWrite(ledPin,1-digitalRead(KeyOK));
if (EncPulse)
  {
  EncPulse=0;
  Choix=ScrollMenu(Menu1);
  }
} 
